package model

import (
	"context"

	"github.com/Masterminds/squirrel"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UmsRoleMenuRelationModel = (*customUmsRoleMenuRelationModel)(nil)

type (
	// UmsRoleMenuRelationModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUmsRoleMenuRelationModel.
	UmsRoleMenuRelationModel interface {
		umsRoleMenuRelationModel
		Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
		RowBuilder() squirrel.SelectBuilder
		CountBuilder(field string) squirrel.SelectBuilder
		DeleteByRoleId(ctx context.Context, roleId int64) error
	}

	customUmsRoleMenuRelationModel struct {
		*defaultUmsRoleMenuRelationModel
	}
)

// NewUmsRoleMenuRelationModel returns a model for the database table.
func NewUmsRoleMenuRelationModel(conn sqlx.SqlConn, c cache.CacheConf) UmsRoleMenuRelationModel {
	return &customUmsRoleMenuRelationModel{
		defaultUmsRoleMenuRelationModel: newUmsRoleMenuRelationModel(conn, c),
	}
}
func (m *defaultUmsRoleMenuRelationModel) DeleteByRoleId(ctx context.Context, roleId int64) error {
	err := m.Delete(ctx, roleId)
	return err
}

// export logic
func (m *defaultUmsRoleMenuRelationModel) Trans(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
	return m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
		return fn(ctx, session)
	})

}

// export logic
func (m *defaultUmsRoleMenuRelationModel) RowBuilder() squirrel.SelectBuilder {
	return squirrel.Select(umsRoleMenuRelationRows).From(m.table)
}

// export logic
func (m *defaultUmsRoleMenuRelationModel) CountBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("COUNT(" + field + ")").From(m.table)
}

// export logic
func (m *defaultUmsRoleMenuRelationModel) SumBuilder(field string) squirrel.SelectBuilder {
	return squirrel.Select("IFNULL(SUM(" + field + "),0)").From(m.table)
}
